#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../liboctopus/src/octopus.h"

struct octopus_context octopus;

void error(void) {
  printf("%s\n", octopus.error_str);
  exit(1);
}

void usage(char *str)
{
  fprintf(stderr, "USAGE %s: mob id string\n\tSchickt string per CAN bei Baudrate von 125K\n", str);
  exit(1);
}

int main(int argc, char *argv[])
{
  int len, id, mob;
  char *str;

  if(argc < 4)
    usage(argv[0]);

  mob = atoi(argv[1]);
  id  = atoi(argv[2]);
  str = argv[3];

  len = strlen(str);
  len = len > 8 ? 8 : len;

  fprintf(stderr, "mod %d, id %d, len %d\n", mob, id, len);

  if(octopus_init (&octopus) <= 0)
    error();

  if(octopus_open(&octopus) <= 0)
    error();

  octopus_can_init(&octopus, 1, 1); // extended id, 125k baudrate
  //i = octopus_can_enable_mob(&octopus, 1, 1, 3, 0xffffffff);
  octopus_can_send_data(&octopus, 1, id, len, str);

  if (octopus_close (&octopus) < 1)
	 printf ("ERROR: %s\n", octopus.error_str);

  return 0;
}
